package com.wt.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wt.admin.entity.WqunArticleTag;
import com.wt.admin.entity.WqunArticleTagType;
import com.wt.admin.service.WqunArticleTagService;
import com.wt.admin.service.WqunArticleTagTypeService;
import com.wt.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 标签类型表 前端控制器
 * </p>
 *
 * @author wt
 * @since 2023-10-13
 */
@Api(tags = "文章标签类型")
@CrossOrigin
@RestController
@RequestMapping("/wqunArticleTagType")
public class WqunArticleTagTypeController {
    @Autowired
    private WqunArticleTagTypeService wqunArticleTagTypeService;

    @ApiOperation("添加文章类型标签")
    @PostMapping("/addArticleTagType")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTagType:addArticleTagType')")
    public R addArticleTag(@RequestBody WqunArticleTagType wqunArticleTagType){
        QueryWrapper<WqunArticleTagType> wqunArticleTagQueryWrapper = new QueryWrapper<>();
        wqunArticleTagQueryWrapper.eq("name",wqunArticleTagType.getName());
        int count = wqunArticleTagTypeService.count(wqunArticleTagQueryWrapper);
        if(count > 0){
            return R.error().message("该标签已经存在");
        }

        boolean save = wqunArticleTagTypeService.save(wqunArticleTagType);
        if(save){
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("修改文章类型标签")
    @PostMapping("/updateArticleTagType")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTagType:updateArticleTagType')")
    public R updateArticleTagType(@RequestBody WqunArticleTagType wqunArticleTagType){
        boolean save = wqunArticleTagTypeService.updateById(wqunArticleTagType);
        if(save){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("删除文章类型标签")
    @PostMapping("/deleteArticleTagType")
    @PreAuthorize("hasAnyAuthority('admin:wqunArticleTagType:deleteArticleTagType')")
    public R deleteArticleTagType(@RequestBody WqunArticleTagType wqunArticleTagType){
        wqunArticleTagType.setIsDelete("1");
        boolean b = wqunArticleTagTypeService.updateById(wqunArticleTagType);
        if(b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }


    @ApiOperation("分页查询文章类型标签")
    @GetMapping("/findAllArticleTagType/{pageSize}/{currentPage}/{selectFind}/{selectFindValue}")
    @PreAuthorize("hasAnyAuthority('wqunArticleTagType:findAllArticleTagType')")
    public R findAllArticleTagType(@PathVariable int pageSize,@PathVariable int currentPage,@PathVariable String selectFind,@PathVariable String selectFindValue){
        return wqunArticleTagTypeService.findAllArticleTagType(pageSize,currentPage,selectFind,selectFindValue);
    }

//    @ApiOperation("查找所有文章类型标签")
//    @GetMapping("/findAllArticleTagType")
//    @PreAuthorize("hasAnyAuthority('wqunArticleTagType:findAllArticleTagType')")
//    public R findAllArticleTagType(){
//        QueryWrapper<WqunArticleTagType> wqunArticleTagTypeQueryWrapper = new QueryWrapper<>();
//        wqunArticleTagTypeQueryWrapper.eq("is_delete","0");
//        List<WqunArticleTagType> findAllArticleTagType = wqunArticleTagTypeService.list(wqunArticleTagTypeQueryWrapper);
//
//        if(findAllArticleTagType != null){
//            return R.ok().data("findAllArticleTagType",findAllArticleTagType);
//        }else{
//            return R.error().message("查询异常");
//        }
//    }

    @ApiOperation("根据id查找所有文章类型标签")
    @GetMapping("/findAllArticleTagTypeById/{id}")
    @PreAuthorize("hasAnyAuthority('wqunArticleTagType:findAllArticleTagTypeById')")
    public R findAllArticleTagTypeById(@PathVariable("id") String id){
        WqunArticleTagType findAllArticleTagTypeById = wqunArticleTagTypeService.getById(id);
        if(findAllArticleTagTypeById != null){
            return R.ok().data("wqunArticleTagType",findAllArticleTagTypeById);
        }else{
            return R.error().message("查询异常");
        }
    }
}

